﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Xml;
using System.Windows.Forms;
using System.Collections;
using DWORD = System.UInt32;
using System.ComponentModel;
using System.Data;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Data;
using Layer;
using _3DTools;
namespace DCSElab_Project
{
    /// <summary>
    /// Interaction logic for SliceViewDlg.xaml
    /// </summary>
    public partial class SliceViewDlg : Window
    {
        ModelVisual3D model;
        Point3D Center;
        ///////////////////////////////////////////////////////////////////////
        ///For transform.

        private bool _mDown;
        private Point _mLastPos;
        private List<Point3D> _point3DList;
        private Layer.LayerSlicing _modelView;
        private double _thickness;
               /////////////////////////////////////////////////////////////////////
        public SliceViewDlg()
        {
            InitializeComponent();
            model = new ModelVisual3D();
            Center = new Point3D();
            _point3DList = new List<Point3D>();
            model.Transform = new Transform3DGroup();
            _modelView = new LayerSlicing();
            _thickness = 1;
              ////////////////////////////////////////////////////////////////////////////
            //Grid Setting
            ///////////////////////////////////////////////////////////////////////////
            System.Drawing.Rectangle rect = Screen.PrimaryScreen.WorkingArea;
            this.Height = rect.Height -100;
            this.Width = rect.Width-300;
            grid.Width = this.Width;
            grid.Height = this.Height;

            slice_viewport.Width = this.Width;
            slice_viewport.Height = this.Height;

            slice_viewport.Children.Add(model);
        }

        private int GetPointNumber()
        {
            return _point3DList.Count;
        }
        public void SetPoint3DList(List<Point3D> point_list)
        {
            _point3DList = point_list;
        }
        public void SetModelView(LayerSlicing view)
        {
            _modelView = view;
        }
        public void SetThickness(double dz)
        {
            _thickness = dz;
        }
     

        private void display_vanvas_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            slice_camera.Width = slice_camera.Width - e.Delta * 3.5 / 250D;

        }

        private void display_vanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {

              if (_mDown==true)
              {
                  Point pos = Mouse.GetPosition(slice_viewport);
                  Point actualPos = new Point(pos.X - slice_viewport.ActualWidth / 2, slice_viewport.ActualHeight / 2 - pos.Y);
                  Double dx = actualPos.X - _mLastPos.X, dy = actualPos.Y - _mLastPos.Y;

                  Double mouseAngle = 0;
                  if (dx != 0 && dy != 0)
                  {
                      mouseAngle = Math.Asin(Math.Abs(dy) / Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)));
                      if (dx < 0 && dy > 0) mouseAngle += Math.PI / 2;
                      else if (dx < 0 && dy < 0) mouseAngle += Math.PI;
                      else if (dx > 0 && dy < 0) mouseAngle += Math.PI * 1.5;
                  }
                  else if (dx == 0 && dy != 0) mouseAngle = Math.Sign(dy) > 0 ? Math.PI / 2 : Math.PI * 1.5;
                  else if (dx != 0 && dy == 0) mouseAngle = Math.Sign(dx) > 0 ? 0 : Math.PI;

                  Double axisAngle = mouseAngle + Math.PI / 2;

                  Vector3D truc = new Vector3D(Math.Cos(axisAngle) * 4, Math.Sin(axisAngle) * 4, 0);

                  Double rotation = 0.01 * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
                  ///////////////////////////////////////////////////////////////////////
       
                  Transform3DGroup trans_model = model.Transform as Transform3DGroup;
          
                  // notich this to create rotation matrix for slicing.      
                  QuaternionRotation3D r_model = new QuaternionRotation3D(new Quaternion(truc, rotation * 180 / Math.PI));
                  //////////////////////////////////////////////////////////////////
                  // For solid model rotation
                  RotateTransform3D rotate_model = new RotateTransform3D(r_model);
                  rotate_model.CenterX = Center.X;
                  rotate_model.CenterY = Center.Y;
                  rotate_model.CenterZ = Center.Z;
                  trans_model.Children.Add(rotate_model);

        
                  _mLastPos = actualPos;
              }     
          }
        private void display_vanvas_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
          {
              if (e.LeftButton != MouseButtonState.Pressed) return;
              _mDown = true;
              Point pos = Mouse.GetPosition(slice_viewport);
              _mLastPos = new Point(pos.X - slice_viewport.ActualWidth / 2, slice_viewport.ActualHeight / 2 - pos.Y);
      
      
          }
        private void display_vanvas_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
          _mDown = false;
        }

        private void play_Click(object sender, RoutedEventArgs e)
        {
            if (_point3DList.Count > 0)
            {
                Utility.SetCamera(slice_camera, _point3DList);
                Center = Utility.GetModelCenter(_point3DList);
                //model.Children.Add(Utility.CreateLine(point3D_list, Colors.DarkCyan));
                List<ModelVisual3D> list_display = Utility.CreateListModelVisual3DComPlex(_point3DList, _thickness, Colors.Azure);
                ModelVisual3D temp = new ModelVisual3D();
                for (int i = 0; i < list_display.Count; i++)
                {
                    temp.Children.Add(list_display[i]);
                }
                model.Children.Add(temp);
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Nothing to show");
                return;
            }
        }

    }
}
